package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.UserMapper;
import cn.cmdr.pojo.User;
import cn.cmdr.service.UserService;
import cn.cmdr.vo.PageVo;
import cn.cmdr.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }

    @Override
    public PageVo getPageUserList(Integer pageNo, Integer pageSize, UserVo userVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
                .like(!StringUtils.isEmpty(userVo.getUserName()),"user_name",userVo.getUserName());
        IPage<User> iPage = super.page(page,queryWrapper);
        return new PageVo(iPage.getRecords(),iPage.getTotal());
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

}
